package com.qf.utils;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/*
 *@Description:
 *@author:刘哥
 *@Date:86180 2022/10/16 22:38
 */
@Component
public class RedisUtils {
    private StringRedisTemplate redisTemplate;
    /**
     * 加锁
     * @param key
     * @param value
     * @param second 锁定时间, 单位为秒
     * @return
     */
    public boolean lock(String key, String value, int second) {
        return redisTemplate.opsForValue().setIfAbsent(key, value, second, TimeUnit.SECONDS);
    }

    /**
     * 解锁
     *
     * @param key
     */
    public void unlock(String key) {
        redisTemplate.delete(key);
    }
}
